package com.roc.web.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.bean.OrginfoSearchBean;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.TblInsInf;
import com.roc.service.IPagenitionService;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigMerinfoUtil;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/org")
public class OrgManageAction extends PaginationBaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Logger logger=Logger.getLogger(OrgManageAction.class);
	private TblInsInf bean;
	private OrgSearchCondition orgSearchCondition;
	private String code;
	private String point;
	private String insCd;
	private String provice32;//省份32域名
	private String city32;//城市32域
	
	
	@Autowired
	private IorgService orgService;
	@Autowired
	IPagenitionService pagenitionService;
	
	/**
	 * 机构管理初始化页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/org/InsQuery.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String init()throws Exception 
	{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.SYSTEM.getCode()))
		{
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;
	}
	
	@Action(value = "search",results = { @Result(name = "success", location = "/pages/org/InsQuery.jsp"),
            @Result(name="error",location="/pages/merchantAdminView.jsp"),
            @Result(name="login",location="/pages/redirect.jsp") })

	public String search(){
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		if(orgSearchCondition==null){
			orgSearchCondition=(OrgSearchCondition) getSearchConditionFromSession();
			if(orgSearchCondition==null){
				orgSearchCondition=new OrgSearchCondition();
			}
		}
		try {
			int rows=pagenitionService.countSQL(orgSearchCondition);
			searchRsList=pagenitionService.findAllSQL(orgSearchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			List<OrginfoSearchBean> orgList=new ArrayList<OrginfoSearchBean>();
            if(searchRsList.size()>0 && searchRsList!=null){
				Iterator iterator=searchRsList.iterator();
				while (iterator.hasNext()) {
					Object[] obj= (Object[])iterator.next();
					OrginfoSearchBean  org=new OrginfoSearchBean(); 
					org.setInsIdCd(Constance.Null2EmptyTrim(obj[0]));
					org.setInsType(Constance.Null2EmptyTrim(obj[1]));
					org.setInsName(Constance.Null2EmptyTrim(obj[2]));
					org.setInsProvCd(ConfigMerinfoUtil.getGBProviceName(Constance.Null2EmptyTrim(obj[3])));
					org.setInsCityCd(ConfigMerinfoUtil.getGBCityName(Constance.Null2EmptyTrim(obj[4])));
                	org.setInsSta(Constance.Null2EmptyTrim(obj[5]));
                	org.setInsStlmTp(Constance.Null2EmptyTrim(obj[6]));
                	org.setInsAloStlmCycle(Constance.Null2EmptyTrim(obj[7]));
                	org.setInsAloStlmMd(Constance.Null2EmptyTrim(obj[8]));
                	org.setInsStlmCNm(Constance.Null2EmptyTrim(obj[9]));
                	org.setInsStlmCAcct(Constance.Null2EmptyTrim(obj[10]));
                	org.setInsStlmCBkNo(Constance.Null2EmptyTrim(obj[11]));
                	org.setInsStlmCBkNm(Constance.Null2EmptyTrim(obj[12]));
                	org.setInsStlmDNm(Constance.Null2EmptyTrim(obj[13]));
                	org.setInsStlmDAcct(Constance.Null2EmptyTrim(obj[14]));
                	org.setInsStlmDBkNo(Constance.Null2EmptyTrim(obj[15]));
                	org.setInsStlmDBkNm(Constance.Null2EmptyTrim(obj[16]));
                	org.setRecUpdOpr(Constance.Null2EmptyTrim(obj[17]));
                	org.setRecCrtTs(Constance.Null2EmptyTrim(obj[18]));
                	org.setRecUpdTs(Constance.Null2EmptyTrim(obj[19]));
                	orgList.add(org);
				}
            }
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, orgList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(orgSearchCondition);
            
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	
	/**
	 * 初始化添加机构
	 * @return
	 */
	@Action(value = "addRuleview", results = { @Result(name = "success", location = "/pages/org/addInsInf.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String addRuleview()throws Exception
	{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.SYSTEM.getCode()))
		{
			getRequest().setAttribute("provinesMap", ConfigProperties.getGBProviceProperties());//32域省份
			getRequest().setAttribute("bankList", ConfigProperties.getSysDB1("UP_BC_CD",false));//银行信息
			Map<String, String> bMap = ConfigProperties.getSysDB1("UP_BC_CD",false);
			getRequest().setAttribute("bMapToList", bMap.values());//银行信息
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;		
	}

	//添加机构
	@Action(value = "addInsInfo", results = { @Result(name = SUCCESS, type = "chain", location = "addRuleview"),
			@Result(name = ERROR, location = "/error.jsp") })
	public String addOrginfo()throws Exception  {
		
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		if(bean==null){
			return SUCCESS;
		}
		try {
			getRequest().setAttribute("provinesMap", ConfigProperties.getUnionProviceProperties());//32域省份
			Map<String, Object> map=orgService.addOrg(bean, op);
			if(map.containsKey("result"))
			{	
				Object obj=map.get("result");
				addActionMessage(String.valueOf(obj));
				return "submitRest";
			}
			addActionMessage("添加成功!");
		
		} catch (ServiceException e) {
			e.printStackTrace();
			addActionError("添加机构信息失败~");
		}
		return SUCCESS;
		
	}
	/**
	 * 初始化机构信息修改
	 * @return
	 */
	@Action(value = "updateOrgJsp", results = { @Result(name = SUCCESS, location = "/pages/org/addInsInf.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String updateOrgJsp()throws Exception
	{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.SYSTEM.getCode()))
		{   
			getRequest().setAttribute("provinesMap", ConfigProperties.getGBProviceProperties());//32域省份
			getRequest().setAttribute("bankList", ConfigProperties.getSysDB1("UP_BC_CD",false));//银行信息
			Map<String, String> bMap = ConfigProperties.getSysDB1("UP_BC_CD",false);
			getRequest().setAttribute("bMapToList", bMap.values());//银行信息
			bean=orgService.queryInsByCode(insCd);
			getRequest().setAttribute("flag", "update");
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;		
	}
	/**
	 * 更改机构信息
	 * @return
	 * @throws Exception 
	 */
	@Action(value = "updateOrg", results = { @Result(name = SUCCESS, location="/pages/org/addInsInf.jsp"),
			@Result(name = "error", location="/error.jsp")})
	public String updateOrg() throws Exception
	{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		try {
			bean.getInsIdCd();
			int count=orgService.updateOrg(bean, op);
			if(count==0)
			{
				addActionError("修改机构信息失败~");
				return ERROR;
			}
			addActionMessage("修改机构信息成功!");
		} catch (ServiceException e) {
			e.printStackTrace();
			addActionError("更改机构信息失败~");
		}
		return SUCCESS;
	}
	
	
	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (OrgSearchCondition)getSession().getAttribute(SEARCH_SESSION_BEAN_ORG);
	}

	
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		return orgSearchCondition;
	}

	
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_BEAN_ORG, searchCondition);
	}
	
	

	public OrgSearchCondition getOrgSearchCondition() {
		return orgSearchCondition;
	}

	public void setOrgSearchCondition(OrgSearchCondition orgSearchCondition) {
		this.orgSearchCondition = orgSearchCondition;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getPoint() {
		return point;
	}
	public void setPoint(String point) {
		this.point = point;
	}
	
	public TblInsInf getBean() {
		return bean;
	}

	public void setBean(TblInsInf bean) {
		this.bean = bean;
	}

	public String getInsCd() {
		return insCd;
	}

	public void setInsCd(String insCd) {
		this.insCd = insCd;
	}
	
	public String getProvice32() {
		return provice32;
	}

	public void setProvice32(String provice32) {
		this.provice32 = provice32;
	}

	public String getCity32() {
		return city32;
	}

	public void setCity32(String city32) {
		this.city32 = city32;
	}

	
}
